﻿using Common.Device.Enums;
using Common.Device.Interfaces;
using Common.Device.Models;
using Common.Device.ViewModels;
using Common.Tool.DataTools;
using Common.View.Tool;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Common.Device.Views
{
    /// <summary>
    /// DianYuanView.xaml 的交互逻辑
    /// </summary>
    public partial class DianYuanView : UserControl
    {
        private DianYuanViewModel viewModel = new DianYuanViewModel();
        public DianYuanView(DeviceEnum.DeviceModelEnum model,Models.Order order=null)
        {
            InitializeComponent();
            //根据仪器型号加载界面
            switch (model)
            {
                case DeviceEnum.DeviceModelEnum.AC11005:
                case DeviceEnum.DeviceModelEnum.AC13003:
                case DeviceEnum.DeviceModelEnum.IT6722A:
                    viewModel.Channels = new System.Collections.ObjectModel.ObservableCollection<int> { 1 };

                    break;
                case DeviceEnum.DeviceModelEnum.DP811:
                    viewModel.Channels = new System.Collections.ObjectModel.ObservableCollection<int> { 1 };

                    break;
                case DeviceEnum.DeviceModelEnum.UDP3305S_E:
                    viewModel.Channels = new System.Collections.ObjectModel.ObservableCollection<int> { 1,2,3 };

                    break;
                default:
                    break;
            }
            DataContext = viewModel;
            if (order != null)
            {
                //根据测试指令刷新界面
                Enum.TryParse(order.temp, out DianYuanEnum.TestMode mode);
                DianYuan parameter = JsonConvert.DeserializeObject<DianYuan>(order.deviceModel);
                switch (mode)
                {
                    case DianYuanEnum.TestMode.设置输出:
                        viewModel.MenuIndex = 0;
                        viewModel.OutChannel = parameter.通道;
                        viewModel.OutVol = parameter.输出电压.ToString();
                        viewModel.OutCur = parameter.输出电流?.ToString();
                        break;
                    case DianYuanEnum.TestMode.读取电压:
                        viewModel.MenuIndex = 1;
                        viewModel.VolChannel = parameter.通道;
                        viewModel.MinVol = parameter.下限值?.ToString();
                        viewModel.MaxVol = parameter.上限值?.ToString();
                        break;
                    case DianYuanEnum.TestMode.读取电流:
                        viewModel.MenuIndex = 2;
                        viewModel.CurChannel = parameter.通道;
                        viewModel.MinCur = parameter.下限值?.ToString();
                        viewModel.MaxCur = parameter.上限值?.ToString();
                        break;
                    case DianYuanEnum.TestMode.读取功率:
                        viewModel.MenuIndex = 3;
                        viewModel.PowerChannel = parameter.通道;
                        viewModel.MinPower = parameter.下限值?.ToString();
                        viewModel.MaxPower = parameter.上限值?.ToString();
                        break;
                    case DianYuanEnum.TestMode.关闭输出:
                        viewModel.MenuIndex = 4;
                        viewModel.CloseChannel = parameter.通道;
                        break;
                    default:
                        break;
                }
            }
        }
        //仪器对象
        public Interfaces.IDianYuan Device;
        private void OutputClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? vol = null; double? cur = null;
            if (!DataConvert.GetDouble(viewModel.OutVol, ref vol) || vol == null)
            {
                View.Controls.Dialogs.TipView.Show("电压输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.OutCur, ref cur))
            {
                View.Controls.Dialogs.TipView.Show("电流输入有误!");
                return;
            }
            //创建参数
            Models.DianYuan parameter = new Models.DianYuan()
            {
                mode = DianYuanEnum.TestMode.设置输出,
                通道 = viewModel.OutChannel,
                输出电压 = (double)vol,
                输出电流 = cur
            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void GetVolClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? min = null; double? max = null;
            if (!DataConvert.GetDouble(viewModel.MaxVol, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.MinVol, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            Models.DianYuan parameter = new Models.DianYuan()
            {
                mode = DianYuanEnum.TestMode.读取电压,
                通道 = viewModel.VolChannel,
                上限值 = max,
                下限值 = min
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void GetCurClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? min = null; double? max = null;
            if (!DataConvert.GetDouble(viewModel.MaxCur, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.MinCur, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            Models.DianYuan parameter = new Models.DianYuan()
            {
                mode = DianYuanEnum.TestMode.读取电流,
                通道 = viewModel.CurChannel,
                上限值 = max,
                下限值 = min
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void CloseClick(object sender, RoutedEventArgs e)
        {
            //创建参数
            Models.DianYuan parameter = new Models.DianYuan()
            {
                mode = DianYuanEnum.TestMode.关闭输出,
                通道 = viewModel.CloseChannel,
            };
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }

        private void GetPowerClick(object sender, RoutedEventArgs e)
        {
            //输入确认
            double? min = null; double? max = null;
            if (!DataConvert.GetDouble(viewModel.MaxPower, ref max))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            if (!DataConvert.GetDouble(viewModel.MinPower, ref min))
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //创建参数
            Models.DianYuan parameter = new Models.DianYuan()
            {
                mode = DianYuanEnum.TestMode.读取功率,
                通道 = viewModel.PowerChannel,
                上限值 = max,
                下限值 = min
            };
            if (parameter.MinMaxCheck() == false)
            {
                View.Controls.Dialogs.TipView.Show("范围输入有误!");
                return;
            }
            //根据当前窗口执行对应方法
            var debug = Tool.CommonHelper.GetDebugView(this);
            if (debug != null && Device != null)
            {
                //调试功能
                Task.Run(() => parameter.GetResult(Device, null));
            }
            var edit = Tool.CommonHelper.GetEditView(this);
            if (edit != null && Device != null)
            {
                //生成指令
                edit.InsertOrder(parameter.GetOrder(Device.Name));
            }
        }
    }
}
